package com.tanhua.admin.service;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.admin.mapper.AnalysisMapper;
import com.tanhua.admin.mapper.LogMapper;
import com.tanhua.model.domain.mysql.Analysis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author lxb哦吼吼
 * @version 1.0
 * @description: TODO
 * @date 2021/11/25 14:52
 */

@Component
public class AnalysisService {

    @Autowired
    private LogMapper logMapper;

    @Autowired
    private AnalysisMapper analysisMapper;

    public void analysis() throws ParseException {

        String todayStr = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        String yestodayStr = DateUtil.yesterday().toString("yyyy-MM-dd");
        //1、统计每日注册用户数
        Integer numRegistered = logMapper.queryByTypeAndLogTime("0102", todayStr);
        //2、统计每日登陆用户
        Integer numLogin = logMapper.queryByTypeAndLogTime("0101", todayStr);
        //3、统计活跃的用户数
        Integer numActive = logMapper.queryByLogTime(todayStr);
        //4、统计次日留存的用户数
        Integer numRetention1d = logMapper.queryNumRetention1d(todayStr, yestodayStr);
        //5、根据当前时间查询AnalysisByDay数据
        QueryWrapper<Analysis> qw = new QueryWrapper<>();
        Date todayDate = new SimpleDateFormat("yyyy-MM-dd").parse(todayStr);
        qw.eq("record_date", todayDate);

        Analysis analysis = analysisMapper.selectOne(qw);
        if (analysis == null) {
            //7、如果不存在，保存
            analysis = new Analysis();
            analysis.setRecordDate(todayDate);
            analysis.setNumRegistered(numRegistered);
            analysis.setNumLogin(numLogin);
            analysis.setNumActive(numActive);
            analysis.setNumRetention1d(numRetention1d);
            analysis.setCreated(new Date());
            analysis.setUpdated(new Date());
            analysisMapper.insert(analysis);
        } else {
            //8、如果存在，更新
            analysis.setNumRegistered(numRegistered);
            analysis.setNumLogin(numLogin);
            analysis.setNumActive(numActive);
            analysis.setNumRetention1d(numRetention1d);
            analysis.setUpdated(new Date());
            analysisMapper.updateById(analysis);
        }

    }

    public Long queryActiveUserCount(DateTime today, DateTime offset) {

        return this.queryAnalysisCount("num_active", offset.toDateStr(), today.toDateStr());
    }

    /**
     * 查询注册用户的数量
     */
    public Long queryRegisterUserCount(DateTime today, DateTime offset) {
        return this.queryAnalysisCount("num_registered", offset.toDateStr(), today.toDateStr());
    }

    /**
     * 查询登录用户的数量
     */
    public Long queryLoginUserCount(DateTime today, DateTime offset) {
        return this.queryAnalysisCount("num_login", offset.toDateStr(), today.toDateStr());
    }

    public Long queryRetention1dCount(DateTime today, DateTime offset) {
        return this.queryAnalysisCount("num_retention1d", offset.toDateStr(), today.toDateStr());
    }

    /**
     * 通用统计查询
     *
     * @param column
     * @param ltDate
     * @param gtDate
     * @return
     */
    public Long queryAnalysisCount(String column, String ltDate, String gtDate) {
        return analysisMapper.sumAnalysisData(column, ltDate, gtDate);
    }
}
